﻿using Client.Constant;
using Client.Enum;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.Service;
using Client.View;
using Client.View.CommonView;
using Client.View.OrgView;
using Client.View.PermissionView;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Client.ViewModel.OrgViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: PermissionEditViewModel
    /// CreateTime: 2020/5/28 19:13:25
    /// </summary>
    public class OrgEditViewModel : BindableBase
    {
        public OrgEdit Page { get; private set; }

        public OrgEditViewModel(OrgEdit page)
        {
            Page = page;

            OrgTreeUserControl = new OrgTree
            {
                DataContext = this
            };

            ParentOrgGotFocusCommand = new DelegateCommand(ParentOrgGotFocus);
            SaveCommand = new DelegateCommand(SaveInfo);
            SelectedOrgCommand = new DelegateCommand<ViewOrg>(SelectedOrg);

            MsgEventSubscribe();

            OrgService.Instance().SubscribeRefreshRootOrgTreeEvent(RefreshOrgTree);
            if (OrgService.Instance().RootOrgTree == null)
            {
                OrgService.Instance().InitOrgTree();
            } else
            {
                OrgTree = OrgService.Instance().RootOrgTree;
            }

        }

        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, ReceiveOptMsg);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(ViewOrg org, OrgListViewModel parentDataContext)
        {
            ParentDataContext = parentDataContext;
            Title = org == null ? "新增组织" : "编辑组织";
            if (org == null)
            {
                ViewOrg = new ViewOrg();
                return;
            }
            ViewOrg = org;
        }

        private ViewOrg org;

        public ViewOrg ViewOrg
        {
            get { return org; }
            set
            {
                org = value;
                RaisePropertyChanged("ViewOrg");
            }
        }


        /// <summary>
        /// 标题
        /// </summary>
        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }


        /// <summary>
        /// 父级页面的DataContext
        /// </summary>
        private OrgListViewModel ParentDataContext { get; set; }

        /// <summary>
        /// 保存组织信息
        /// </summary>
        public DelegateCommand SaveCommand { get; private set; }
        public void SaveInfo()
        {
            AddEditOrgReq msg = new AddEditOrgReq(MessageId.ADD_EDIT_ORG)
            {
                Org = ViewOrg
            };
            ApplicationClient.Instance().Send(msg);
        }

        /// <summary>
        /// 操作回复消息接收
        /// </summary>
        /// <param name="message">回复消息</param>
        private void ReceiveOptMsg(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                if (msg.OptType != (int)OptType.ADD_EDIT_ORG)
                {
                    return;
                }
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ConfirmTip tip = new ConfirmTip()
                    {
                        Message = { Text = msg.Content }
                    };
                    DialogHost.Show(tip, "OrgEditPage", CloseTip);
                }));
            }
        }


        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            Page.Hide();
            if (ParentDataContext != null)
            {
                OrgService.Instance().InitOrgTree();    // 刷新内存中组织树
                ParentDataContext.GetOrgs();
            }
        }


        /// <summary>
        /// 父级组织文本框绑定命令
        /// </summary>
        public DelegateCommand ParentOrgGotFocusCommand { get; set; }
        private void ParentOrgGotFocus()
        {
            DialogHost.Show(OrgTreeUserControl, "OrgEditPage", CloseSelectParentPanel);
        }


        public void CloseSelectParentPanel(object sender, DialogClosingEventArgs eventArgs)
        {
            if (string.Equals(eventArgs.Parameter, "Cancel"))
            {
                // 取消逻辑

            } else
            {
                if (ViewOrg.ID != 0 && ViewOrg.ID == SelectedParentOrg.ID)
                {
                    return;
                }
                // 确定逻辑
                ViewOrg.ParentId = SelectedParentOrg.ID;
                ViewOrg.ParentOrgName = selectedParentOrg.OrgName;
            }
        }


        private ViewOrg selectedParentOrg;

        public ViewOrg SelectedParentOrg
        {
            get { return selectedParentOrg; }
            set
            {
                selectedParentOrg = value;
                RaisePropertyChanged("SelectedParentOrg");
            }
        }


        public OrgTree OrgTreeUserControl { get; set; }

        private void RefreshOrgTree(List<ViewOrg> orgTree)
        {
            OrgTree = orgTree;
        }

        private List<ViewOrg> orgTree;

        public List<ViewOrg> OrgTree
        {
            get { return orgTree; }
            set
            {
                orgTree = value;
                RaisePropertyChanged("OrgTree");
            }
        }


        public DelegateCommand<ViewOrg> SelectedOrgCommand { get; set; }
        private void SelectedOrg(ViewOrg org)
        {
            if (ViewOrg.ID != 0 && ViewOrg.ID == org.ID)
            {
                WarnTip = Visibility.Visible;
                return;
            } else
            {
                WarnTip = Visibility.Hidden;
            }
            SelectedParentOrg = org;
        }


        private Visibility warnTip = Visibility.Hidden;

        public Visibility WarnTip
        {
            get { return warnTip; }
            set
            {
                warnTip = value;
                RaisePropertyChanged("WarnTip");
            }
        }


    }
}
